<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <title>用户列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="resources/layui/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="resources/layui/css/index.css" media="all" />
    <!-- 引入 echarts.js -->
    <script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script>
</head>
<body class="childrenBody">
<form class="layui-form">
    <div class="layui-inline">
        <label class="layui-form-label">车牌号:</label>
        <div class="layui-input-block" style="width: 150px;">
            <select id="carNo" name="select" class="select" lay-verify="required" lay-filter="carNo" lay-search>
                <option value="">请选择或搜索</option>
            </select>
        </div>
    </div>
    <!-- 为 ECharts 准备一个具备大小（宽高）的 DOM -->
    <div id="main" class="layui-inline" style="width:720px;height:480px;position:absolute;top:10%;left: 15%;margin-top: 0px;margin-left: 0px;"></div>
</form>
<script type="text/javascript" src="resources/layui/layui/layui.js"></script>
<script th:inline="none">
    layui.use(['form','layer','laydate','table','laytpl','jquery'],function() {
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            laydate = layui.laydate,
            laytpl = layui.laytpl,
            table = layui.table;

        //异步加载下拉框数据
        $.get("cars/selectAllCars.do",function (obj) {
            var data = JSON.stringify(obj);
            var $html = "";
            if (data != null) {
                $.each(JSON.parse(data), function (index, item) {
                    $html += "<option value='" + item.id + "'>" + item.car_no + "</option>";

                });
                $("#carNo").append($html);
                //append后必须从新渲染
                form.render('select');
            }
        });

        //监听选择车牌号下拉框
        form.on('select(carNo)', function(obj){
            var data = JSON.stringify(obj);
            // 基于准备好的dom，初始化echarts实例
            var myChart = echarts.init(document.getElementById('main'));//main是<div id="main" style="width: 1050px;height:700px;"></div>的id
            // 指定图表的配置项和数据
            var id =$('#carNo').find('option:selected').val();
            var option = {
                title: {
                    text: '车辆费用分布',
                    subtext: '单个车辆',
                    x: 'center'
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient: 'vertical',
                    left: 'left',
                    // data: []
                },
                series: [
                    {
                        name: '费用(￥)|占比(%)',
                        type: 'pie',
                        radius: '55%',
                        center: ['50%', '60%'],
                        data:(function(){
                            var datas = [];
                            $.ajax({
                                type: "GET",
                                url: "/cars/showInfo.do?id="+id,
                                dataType: 'json',
                                async: false,
                                success: function(result) {
                                    for(var i=0; i<result.length; i++) {
                                        datas.push({"value":result[i].amount, "name":result[i].type})
                                    }
                                }
                            })
                            return datas;
                        })(),
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
            // 使用刚指定的配置项和数据显示图表。
            myChart.setOption(option);
        });
    })
</script>
</body>
</html>